﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SQLite;

namespace WebLog
{
    public class LogDataBase
    {
        private string connectionString;
        private SQLiteConnection connection = null;
        private SQLiteCommand command = null;

        public LogDataBase()
        {
            Configure();
        }

        private void Configure()
        {
            try
            {
                connectionString = ConfigurationManager.ConnectionStrings["log"].ConnectionString;

            }
            catch (Exception ex)
            {

            }
        }

        public void Connect()
        {
            if (connection != null && !connection.State.Equals(ConnectionState.Closed))
            {
                Exception ex = new ApplicationException("Conexion Abierta");
            }
            try
            {
                if (this.connection == null)
                {
                    connection = new SQLiteConnection();
                    connection.ConnectionString = string.Format("Data Source={0};New=True;Compress=True;Synchronous=Off", connectionString); 
                }
                connection.Open();
            }
            catch (Exception ex)
            {

            }
        }

        public void Disconnect()
        {
            if (connection.State.Equals(ConnectionState.Open))
            {
                connection.Close();
            }
        }

        public void ReleaseResources()
        {
            connection.Dispose();
        }

        public void CreateSql(string sql)
        {
            command = new SQLiteCommand();
            command.Connection = connection;
            command.CommandType = CommandType.Text;
            command.CommandText = sql;

        }

        public SQLiteDataReader ExecuteDataReader()
        {
            return command.ExecuteReader();
        }
    }
}
